//
//  CustomLabel.swift
//  test
//
//  Created by 寇凤伟 on 16/2/29.
//  Copyright © 2016年 FoxHound. All rights reserved.
//

import Foundation
import UIKit

class CustomLabel : UILabel{
    enum VerticalAlignment:Int
    {
        case VerticalAlignmentTop = 0 // default
        case VerticalAlignmentMiddle
        case VerticalAlignmentBottom
    }
    var verticalAlignment:VerticalAlignment
    
    required init(coder aDecoder: NSCoder) {
         verticalAlignment=VerticalAlignment.VerticalAlignmentTop
        super.init(coder: aDecoder)!
    }
    
    override init(frame: CGRect) {
         verticalAlignment=VerticalAlignment.VerticalAlignmentTop
        super.init(frame: frame)
    }
    
    
    override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
        let rect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
        var result:CGRect;
        switch (verticalAlignment)
        {
        case VerticalAlignment.VerticalAlignmentTop:
            result=CGRect.init(x: bounds.origin.x, y:bounds.origin.y , width: rect.size.width, height: rect.size.height)
            
        case VerticalAlignment.VerticalAlignmentMiddle:
            result=CGRect.init(x: bounds.origin.x, y: bounds.origin.y + (bounds.size.height - rect.size.height) / 2, width: rect.size.width, height: rect.size.height)
            
        case VerticalAlignment.VerticalAlignmentBottom:
            result=CGRect.init(x: bounds.origin.x, y:bounds.origin.y + (bounds.size.height - rect.size.height), width: rect.size.width, height: rect.size.height)
           
            
        }
         return result
    }
    
    override func drawText(in rect: CGRect) {
        let r=self.textRect(forBounds: rect, limitedToNumberOfLines: self.numberOfLines)
        super.drawText(in: r)
    }
    
}
